<template>
  <div class='app-container calendar-list-container'>
    <div class='filter-container'>
    <org-tree :dcksValue.sync="listQuery.orgCode"  :dcksLabel.sync="listQuery.orgDescr" :checkStrictly=true :isDialog="true"></org-tree>
       <!-- <el-select v-model="listQuery.enable" placeholder="问题状态" clearable >
        <el-option label="无效" value="0"></el-option>
        <el-option label="有效" value="1"></el-option>
      </el-select> -->
      <el-button class='filter-item' type='primary' v-waves icon='search' @click='handleFilter'>搜索</el-button>
      <el-button class="filter-item" type="primary" icon="document" @click="handleDownload">导出</el-button>
    </div>
    <el-table height="400" @selection-change="selsChange" :key='tableKey' :data='list' border fit highlight-current-row style='width: 100%'>
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column label='状态' align="center" width="100">
        <template scope="scope">
          <el-tag size="medium" :type="scope.row.enable === '1' ? 'success' : 'error'">{{scope.row.enable | enableFilter}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label='问题种类' prop="type" width="100">
      </el-table-column>
      <el-table-column label='问题描述' prop="content" width="100">
      </el-table-column>
    </el-table>
    <div class='pagination-container'>
      <el-pagination @size-change='handleSizeChange' @current-change='handleCurrentChange' :current-page.sync='listQuery.page'
        :page-sizes='[10,20,30,50]' :page-size='listQuery.limit' layout='total, sizes, prev, pager, next, jumper' :total='total'>
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :fullscreen="true">
      <el-form class="small-space" :model="temp" :rules="rules" ref="form" label-position="left" label-width="70px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="状态" prop="enable">
          <el-select v-model="temp.enable" clearable placeholder="请选择">
            <el-option v-for="item in enableOptions" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="问题种类" prop="type">
        <el-input v-model="temp.type"></el-input>
          </el-form-item>
         <el-form-item label="问题描述" prop="content">
          <el-input v-model="temp.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!-- <el-button v-if="dialogStatus=='create'" type="primary" @click="create">确 定</el-button>
        <el-button v-else-if="dialogStatus=='copy'" type="primary" @click="copy">确 定</el-button>
        <el-button v-else type="primary" @click="update">确 定</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from "@/api/issue";
import waves from "@/directive/waves/index.js"; // 水波纹指令
import orgTree from "@/components/OrgTree/orgTree";
// import { mapGetters } from "vuex";

export default {
  name: "x",
  directives: {
    waves
  },
  components: { orgTree},
  computed: {
    // ...mapGetters([
    //   "name",
    //   "roles",
    //   "myWorkShop"
    // ])
  },
  data() {
    return {
      sels: [],
      list: null,
      showAuditor: false,
      total: null,
      listQuery: {
        page: 1,
        limit: 20,
        type: undefined,
        enable: undefined,
        sort: "+id",
        orgCode:[],
        orgDescr: []
      },
      enableOptions: [
        { value: "0", label: "无效" },
        { value: "1", label: "有效" }
      ],
      typeOptions: [
        { value: "0", label: "月计划" },
        { value: "1", label: "临时计划" }
      ],
      temp: {
        id: undefined,
        enable: "",
        type: "",
        content: ""
      },
      tag: "",
      key: [],
      checkids: { ids: "" },
      delids: { ids: "" },
      cpids: { ids: "" },
      textMap: {
        // update: "编辑",
        // create: "创建",
        // copy: "复制"
      },
      dialogFormVisible: false,
      dialogStatus: "",
      tableKey: 0,
      rules: {
               content: [
          {
            required: true,
            max: 800,
            message: "长度小于800个字符",
            trigger: "blur"
          }
        ]
      },
    };
  },
  created() {
    this.getList();
  },

  filters: {
    enableFilter: function(value) {
      if ("0" === value) {
        return "无效";
      } else if ("1" === value) {
        return "有效";
      } 
    },
    
    typeFilter: function(value) {
      if ("0" === value) {
        return "月计划";
      } else if ("1" === value) {
        return "临时计划";
      }
    }
  },
  methods: {
    getList() {
      this.$store.dispatch("openLoading");
      fetchList(this.listQuery).then(response => {
        // debugger
        // console.log(response.data);
        if (response.status === 200 && response.data.code === 2000) {
          console.log("response.data.items", response.data.items);
          this.list = response.data.items;
          this.total = response.data.total;
        } else {
          console.log(response);
          this.$notify({
            title: "失败1",
            message: response.status + response.data.msg,
            type: "error",
            duration: 200000
          });
        }
        this.$store.dispatch("closeLoading");
      });
    },
    dateSechChange(val) {
      this.listQuery.date = val;
    },

    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },

    selsChange(sels) {
      this.sels = sels;
    },
    
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },

    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = [
             "ID",
          "状态",
          "问题种类",
          "问题描述"
        ];
        const filterVal = [
            "id",
            "enable",
            "type",
            "content"
        ];
        const data = this.formatJson(filterVal, this.list);
        excel.export_json_to_excel(tHeader, data, "培训计划");
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "tel") {
            return "不可下载";
          } else {
            // 计划状态
            if (j === "enable") {
              for (const x of this.enableOptions) {
                if (x.value === v[j]) {
                  return x.label;
                  break;
                }
              }
            }
            
            return v[j];
          }
        })
      );
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
  }
};
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>